<?php

/*
	info: 商家控制面板
	date: 2011-12-19
*/

define('PAGESCRIPT', 'mycom');
define('USER_LOGIN_CHECK', TRUE);
require dirname(__FILE__).'/include/common.inc.php';
global_ConnectDB();

if ($user['base_uid']<=0 || $user['base_type']!=1)
{
	global_ShowMessage("请登录后操作。", WEBURLPATH.'/login.php');
}
//优惠券列表
if ($webglobal['action']=='couponlist') {
	$webglobal['coupon_status'] = array('-1'=>'删除',0=>'待审核',1=>'审核通过',2=>'未通过');

	$webglobal['file_coupon'] = array();
	$webglobal['file_coupon']['perpage'] = 10;
	$webglobal['file_coupon']['datanum'] = $db->GetNum("SELECT COUNT(*) FROM {$webconfig['dbpre']}coupon WHERE company_id = " . $user['base_uid'] . " AND status >= 0");

	if ($webglobal['file_coupon']['datanum'] > 0) {
		$curoptions['page'] = '@@';
		$webglobal['page'] = global_Page($webglobal['file_coupon']['datanum'], $webglobal['file_coupon']['perpage'], $webrequest['page'], WEBURLPATH."/mycom.php?action=couponlist&page=@@");

		$tempstartnum = ($webrequest['page']-1)*$webglobal['file_coupon']['perpage'];
		$temporderstr = "pubtime DESC";

		$query = $db->Query("SELECT * FROM {$webconfig['dbpre']}coupon WHERE company_id = '{$user['base_uid']}' AND status >= 0 LIMIT $tempstartnum, {$webglobal['file_coupon']['perpage']}");
		$temptr = 1;
		$webglobal['file_coupon']['dataarray'] = array();

		while ($tempdata = $db->FetchArray($query)) {
			$tempdata['starttime_show'] = !empty($tempdata['starttime']) ? global_FormatTime($tempdata['starttime'], 'FD') : '';
			$tempdata['endtime_show'] = !empty($tempdata['endtime']) ? global_FormatTime($tempdata['endtime'], 'FD') : '';
			$tempdata['pubtime_show'] = !empty($tempdata['pubtime']) ? global_FormatTime($tempdata['pubtime']) : '';
			$webglobal['file_coupon']['dataarray'][$tempdata['id']] = $tempdata;
		}
	}
} elseif ($webglobal['action'] == 'couponedit') {
	$webrequest['id'] = isset($webrequest['id']) ? intval($webrequest['id']) : 0;

	if ($webrequest['id'] <= 0) {
		admin_ShowMessage('不存在该优惠劵。', '', 'BACK');
	}

	$webglobal['file_coupon']['editinfo'] = $db->GetOne("SELECT * FROM {$webconfig['dbpre']}coupon WHERE id={$webrequest['id']} AND company_id = '{$user['base_uid']}'");

	if (!$webglobal['file_coupon']['editinfo']) {
		global_ShowMessage('不存在该优惠劵。', '', 'BACK');
	}
	if ($webglobal['file_coupon']['editinfo']['status'] != 0) {
		global_ShowMessage('只有未审核的优惠劵可以进行修改。', '', 'BACK');
	}

	$webglobal['file_coupon']['category'] = $couponCategoryDataModel->formatCategoryOption(0,0);
	$webglobal['file_coupon']['imagepath'] = '/upfiles/coupon/'.global_FormatTime(0, 'Ym');
	$webglobal['file_coupon']['logosetting'] = array('type'=>array('gif', 'jpg', 'jpeg', 'png', 'bmp'), 'size'=>2097152);

	if (isset($webrequest['check'])) {
		include_once WEBFILEPATH.'/include/upload.class.php';
		global_CheckPost(
				array(
					array('coupon_category_id,','int','isset($webglobal[\'file_coupon\'][\'category\'][@])','请选择优惠劵分类',1),
					array('name,','str','','优惠劵名称不能为空',1,''),
					array('original,', 'num', '', '原价格式错误', 1, '0.00'),
					array('price,', 'num', '', '优惠价格式错误', 1, '0.00'),
					array('starttime', 'date', '', '开始时间必须为日期格式', 1),
					array('endtime', 'date', '', '结束时间必须为日期格式', 1),
					array('introduction,','','','优惠劵介绍不能为空',1),
			));

		//其他要求判断
		if (empty($webglobal['CPM']))
		{
			$webglobal['CPP']['starttime'] .= ' 00:00:00';
			$webglobal['CPP']['endtime'] .= ' 23:59:59';
			if (global_ConvTime($webglobal['CPP']['starttime']) >= global_ConvTime($webglobal['CPP']['endtime'])) {
				$webglobal['CPM']['starttime'] = '开始时间不能大于结束时间';
			}
		}
		//上传判断
		if (empty($webglobal['CPM']))
		{
			$templogoupload = new Upload('image', $webglobal['file_coupon']['logosetting']);
			if (!$templogoupload->CheckFile() && !$templogoupload->isNone)
			{
				$webglobal['CPM']['logo'] = '优惠劵图片: '.$templogoupload->errorMsg;
			}
		}
		//检查完毕保存
		if (empty($webglobal['CPM']))
		{
			$webglobal['CPD']['starttime'] = global_ConvTime($webglobal['CPP']['starttime']);
			$webglobal['CPD']['endtime'] = global_ConvTime($webglobal['CPP']['endtime']);
			$webglobal['CPD']['introduction'] = global_ConvNewline($webglobal['CPD']['introduction']);

			if ($templogoupload->CheckFile())
			{
				$templogoupload->SaveFile(WEBFILEPATH.$webglobal['file_coupon']['imagepath']);
				$webglobal['CPD']['image'] = $webglobal['file_coupon']['imagepath'].'/'.$templogoupload->fileList[0]['name'];

				//生成缩略图
				$imageFilePath = WEBFILEPATH.$webglobal['file_coupon']['imagepath'] . '/' . $templogoupload->fileList[0]['name'];

				include WEBFILEPATH.'/include/image.class.php';
				$objImg = new Image();
				//Big Image:707 325
				//middle Image:188 204
				//small Image:205 133

				$bigImage = WEBFILEPATH.$webglobal['file_coupon']['imagepath'] . '/big_' . $templogoupload->fileList[0]['name'];
				$middleImage = WEBFILEPATH.$webglobal['file_coupon']['imagepath'] . '/middle_' . $templogoupload->fileList[0]['name'];
				$smallImage = WEBFILEPATH.$webglobal['file_coupon']['imagepath'] . '/small_' . $templogoupload->fileList[0]['name'];

				$objImg->OnceResize($imageFilePath , 707 , 325 , $bigImage, 0, FALSE);
				$objImg->OnceResize($imageFilePath , 188 , 204 , $middleImage, 5, FALSE);
				$objImg->OnceResize($imageFilePath , 205 , 133 , $smallImage, 5, FALSE);

				if ($webglobal['file_coupon']['editinfo']['image'] != '')
				{
					@unlink(WEBFILEPATH.$webglobal['file_coupon']['editinfo']['image']);
				}
			}
			$db->Update('coupon', '', 'id='.$webrequest['id']);
			cache_GetCouponDetail($webrequest['id'], false, true);
			global_ShowMessage("优惠劵修改成功", WEBURLPATH."/mycom.php?action=couponlist");
		}
		else
		{
			global_ShowMessage($webglobal['CPM'], '', 'BACK');
		}
	} else {
		$webglobal['file_coupon']['editinfo']['starttime_show'] = !empty($webglobal['file_coupon']['editinfo']['starttime']) ? global_FormatTime($webglobal['file_coupon']['editinfo']['starttime'], 'FD') : '';
		$webglobal['file_coupon']['editinfo']['endtime_show'] = !empty($webglobal['file_coupon']['editinfo']['endtime']) ? global_FormatTime($webglobal['file_coupon']['editinfo']['endtime'], 'FD') : '';
	}
} elseif ($webglobal['action'] == 'coupondel') {
	$webrequest['id'] = isset($webrequest['id']) ? intval($webrequest['id']) : 0;

	if ($webrequest['id'] <= 0) {
		global_ShowMessage('没有需要删除的优惠劵。', '', 'BACK');
	}
	$webglobal['file_coupon']['editinfo'] = $db->GetOne("SELECT * FROM {$webconfig['dbpre']}coupon WHERE id={$webrequest['id']} AND company_id = '{$user['base_uid']}'");
	if (!$webglobal['file_coupon']['editinfo']) {
		global_ShowMessage('不存在该优惠劵。', '', 'BACK');
	}

	$updateData = array(
		'status' => -1,
	);
	$db->Update('coupon', $updateData, 'id='.$webrequest['id']);

	cache_GetCouponDetail($webrequest['id'], false, true);
	cache_GetNewCoupon(true);

	global_ShowMessage("优惠劵删除成功", WEBURLPATH."/mycom.php?action=couponlist");

//优惠券添加
} elseif ($webglobal['action']=='couponadd') {
	$webglobal['file_coupon']['category'] = $couponCategoryDataModel->formatCategoryOption(0,0);
	$webglobal['file_coupon']['imagepath'] = '/upfiles/coupon/'.global_FormatTime(0, 'Ym');
	$webglobal['file_coupon']['logosetting'] = $webglobal['file_register']['bcodesetting'] = array('type'=>array('gif', 'jpg', 'jpeg', 'png', 'bmp'), 'size'=>2097152);

	if (isset($webrequest['check'])) {
		include_once WEBFILEPATH.'/include/upload.class.php';
		global_CheckPost(
				array(
					array('coupon_category_id,','int','isset($webglobal[\'file_coupon\'][\'category\'][@])','请选择优惠劵分类',1),
					array('name,','str','','优惠劵名称不能为空',1,''),
					array('original,', 'num', '', '原价格式错误', 1, '0.00'),
					array('price,', 'num', '', '优惠价格式错误', 1, '0.00'),
					array('starttime', 'date', '', '开始时间必须为日期格式', 1),
					array('endtime', 'date', '', '结束时间必须为日期格式', 1),
					array('introduction,','','','优惠劵介绍不能为空',1),
			));

		//其他要求判断
		if (empty($webglobal['CPM']))
		{
			$webglobal['CPP']['starttime'] .= ' 00:00:00';
			$webglobal['CPP']['endtime'] .= ' 23:59:59';
			if (global_ConvTime($webglobal['CPP']['starttime']) >= global_ConvTime($webglobal['CPP']['endtime'])) {
				$webglobal['CPM']['starttime'] = '开始时间不能大于结束时间';
			}
		}
		//上传判断
		if (empty($webglobal['CPM']))
		{
			$templogoupload = new Upload('image', $webglobal['file_coupon']['logosetting']);
			if (!$templogoupload->CheckFile() && !$templogoupload->isNone)
			{
				$webglobal['CPM']['logo'] = '优惠劵图片: '.$templogoupload->errorMsg;
			}
		}
		//检查完毕保存
		if (empty($webglobal['CPM']))
		{
			$webglobal['CPD']['starttime'] = global_ConvTime($webglobal['CPP']['starttime']);
			$webglobal['CPD']['endtime'] = global_ConvTime($webglobal['CPP']['endtime']);
			$webglobal['CPD']['introduction'] = global_ConvNewline($webglobal['CPD']['introduction']);
			if ($templogoupload->CheckFile())
			{
				$templogoupload->SaveFile(WEBFILEPATH.$webglobal['file_coupon']['imagepath']);
				$webglobal['CPD']['image'] = $webglobal['file_coupon']['imagepath'].'/'.$templogoupload->fileList[0]['name'];

				//生成缩略图
				$imageFilePath = WEBFILEPATH.$webglobal['file_coupon']['imagepath'] . '/' . $templogoupload->fileList[0]['name'];

				include WEBFILEPATH.'/include/image.class.php';
				$objImg = new Image();

				//Big Image:707 325
				//middle Image:188 204
				//small Image:205 133

				$bigImage = WEBFILEPATH.$webglobal['file_coupon']['imagepath'] . '/big_' . $templogoupload->fileList[0]['name'];
				$middleImage = WEBFILEPATH.$webglobal['file_coupon']['imagepath'] . '/middle_' . $templogoupload->fileList[0]['name'];
				$smallImage = WEBFILEPATH.$webglobal['file_coupon']['imagepath'] . '/small_' . $templogoupload->fileList[0]['name'];

				$objImg->OnceResize($imageFilePath , 707 , 325 , $bigImage, 0, FALSE);
				$objImg->OnceResize($imageFilePath , 188 , 204 , $middleImage, 5, FALSE);
				$objImg->OnceResize($imageFilePath , 205 , 133 , $smallImage, 5, FALSE);

			}
			$webglobal['CPD']['company_id'] = $user['base_uid'];
			$webglobal['CPD']['pubtime'] = $webserver['timestamp'];
			$webglobal['CPD']['comment_total_score'] = 0;
			$webglobal['CPD']['comment_total_time'] = 0;
			$webglobal['CPD']['favorites_num'] = 0;
			$webglobal['CPD']['status'] = 0;
			$tempadduid = $db->Insert('coupon', '', TRUE);
			//cache_GetNewCoupon(true);
			//修改对应的商家的缓存数据
			global_ShowMessage("恭喜您！优惠价添加成功。优惠劵需要审核，审核通过后系统会通过邮件通知，谢谢！", WEBURLPATH."/mycom.php?action=couponlist");
		}
		else
		{
			global_ShowMessage($webglobal['CPM'], '', 'BACK');
		}
	}
} elseif ($webglobal['action']=='image') {
	$webglobal['file_company'] = array();
	$webglobal['file_company']['editinfo'] = $user;
	$webglobal['file_company']['imagepath'] = '/upfiles/company/'.global_FormatTime(0, 'Ym');
	$webglobal['file_company']['logosetting'] = $webglobal['file_company']['bcodesetting'] = array('type'=>array('gif', 'jpg', 'jpeg', 'png', 'bmp'), 'size'=>2097152);

	if (isset($webrequest['check']))
	{
		include WEBFILEPATH.'/include/upload.class.php';

		if (empty($webglobal['CPM'])) {
			$templogoupload = new Upload('logo', $webglobal['file_company']['logosetting']);
			if (!$templogoupload->CheckFile() && !$templogoupload->isNone) {
				$webglobal['CPM']['logo'] = 'logo图片: '.$templogoupload->errorMsg;
			}

			$tempimageupload = new Upload('image', $webglobal['file_company']['logosetting']);
			if (!$tempimageupload->CheckFile() && !$tempimageupload->isNone) {
				$webglobal['CPM']['image'] = '形象图片: '.$tempimageupload->errorMsg;
			}
		}

		if (!empty($webglobal['CPM'])) {
			global_ShowMessage($webglobal['CPM'], '', 'BACK');
		} else {
			include WEBFILEPATH.'/include/image.class.php';
			if ($templogoupload->CheckFile()) {
				$templogoupload->SaveFile(WEBFILEPATH.$webglobal['file_company']['imagepath']);
				$webglobal['CPD']['logo'] = $webglobal['file_company']['imagepath'].'/'.$templogoupload->fileList[0]['name'];
				if ($webglobal['file_company']['editinfo']['logo'] != '') {
					@unlink(WEBFILEPATH.$webglobal['file_company']['editinfo']['logo']);
				}
				//生成缩略图
				$imageFilePath = WEBFILEPATH.$webglobal['file_company']['imagepath'] . '/' . $templogoupload->fileList[0]['name'];
				$objImg = new Image();
				//Big Image:205 133
				$smallImage = WEBFILEPATH.$webglobal['file_company']['imagepath'] . '/small_' . $templogoupload->fileList[0]['name'];
				$objImg->OnceResize($imageFilePath , 100 , 100 , $smallImage, 5, FALSE);
			}
			
			if ($tempimageupload->CheckFile()) {
				$tempimageupload->SaveFile(WEBFILEPATH.$webglobal['file_company']['imagepath']);
				$webglobal['CPD']['image'] = $webglobal['file_company']['imagepath'].'/'.$tempimageupload->fileList[0]['name'];
				if ($webglobal['file_company']['editinfo']['image'] != '') {
					@unlink(WEBFILEPATH.$webglobal['file_company']['editinfo']['image']);
				}

				//生成缩略图
				$imageFilePath = WEBFILEPATH.$webglobal['file_company']['imagepath'] . '/' . $tempimageupload->fileList[0]['name'];
				$objImg = new Image();
				//Big Image:205 133
				$bigImage = WEBFILEPATH.$webglobal['file_company']['imagepath'] . '/big_' . $tempimageupload->fileList[0]['name'];
				$middleImage = WEBFILEPATH.$webglobal['file_company']['imagepath'] . '/middle_' . $tempimageupload->fileList[0]['name'];
				$smallImage = WEBFILEPATH.$webglobal['file_company']['imagepath'] . '/small_' . $tempimageupload->fileList[0]['name'];

				$objImg->OnceResize($imageFilePath , 205 , 133 , $bigImage, 5, FALSE);
				$objImg->OnceResize($imageFilePath , 195 , 195 , $middleImage, 5, FALSE);
				$objImg->OnceResize($imageFilePath , 50 , 50 , $smallImage, 5, FALSE);
			}

			$db->Update('company', '', "id='{$webglobal['file_company']['editinfo']['id']}'");
			cache_GetNewCompany(false, true);
			//更新缓存
			cache_GetCompanyDetail($webglobal['file_company']['editinfo']['id'], true, true);

			global_ShowMessage("恭喜您，资料更新成功！", WEBURLPATH."/mycom.php?action=image");
		}
	}
} elseif ($webglobal['action']=='password') {
	$webglobal['file_company'] = array();
	$webglobal['file_company']['editinfo'] = $user;

	if (isset($webrequest['check']))
	{
		include WEBFILEPATH.'/include/upload.class.php';
		global_CheckPost(
				array(
					array('passwordold','password','','旧密码输入错误',0,''),
					array('password','password','','新密码至少为6位',0,''),
					array('passwordtwo','password','','新密码重复至少为6位',0,''),
			));

		if (empty($webglobal['CPM']))
		{
			if ($webglobal['CPP']['password']!='')
			{
				if (global_Md5($webglobal['CPP']['passwordold'])!=$webglobal['file_company']['editinfo']['pwd'])
				{
					$webglobal['CPM']['passwordold'] = '旧密码输入错误';
				}
				elseif ($webglobal['CPP']['password']!=$webglobal['CPP']['passwordtwo'])
				{
					$webglobal['CPM']['passwordtwo'] = '两次输入的密码不一样';
				}
				else
				{
					$webglobal['CPD']['pwd'] = global_Md5($webglobal['CPP']['password']);
				}
			}
		}

		if (!empty($webglobal['CPM'])) {
			global_ShowMessage($webglobal['CPM'], '', 'BACK');
		} else {
			$db->Update('company', '', "id='{$webglobal['file_company']['editinfo']['id']}'");
			if (isset($webglobal['CPD']['pwd']))
			{
				$usercheckobj->SetUserCookie($webglobal['file_company']['editinfo']['id'], $webglobal['CPD']['pwd'], 1);
			}
			//更新缓存
			cache_GetCompanyDetail($webglobal['file_company']['editinfo']['id'], true, true);

			global_ShowMessage("恭喜您，密码修改成功！", WEBURLPATH."/mycom.php?action=password");
		}
	}
} elseif ($webglobal['action']=='map') {
	$webglobal['file_company'] = array();
	$webglobal['file_company']['editinfo'] = $user;

	//$webglobal['file_company']['editinfo']['areainfo'] = ;
	$areaInfo = '';
	if ($webglobal['file_company']['editinfo']['province'] > 0) {
		$areaInfo .= $webglobal['areadata'][$webglobal['file_company']['editinfo']['province']]['name'];
	}
	if ($webglobal['file_company']['editinfo']['city'] > 0) {
		$areaInfo .= $webglobal['areadata'][$webglobal['file_company']['editinfo']['city']]['name'];
	}
	$webglobal['file_company']['editinfo']['areainfo'] = $areaInfo;

	if (isset($webrequest['check']))
	{
		include WEBFILEPATH.'/include/upload.class.php';
		global_CheckPost(
				array(
					array('latitude,','','preg_match("/^[0-9]+\.[0-9]+$/", @)','',0,0),
					array('longitude,','','preg_match("/^[0-9]+\.[0-9]+$/", @)','',0,0),
			));

		if (!empty($webglobal['CPM'])) {
			global_ShowMessage($webglobal['CPM'], '', 'BACK');
		}
		else
		{
			$db->Update('company', '', "id='{$webglobal['file_company']['editinfo']['id']}'");
			//更新缓存
			cache_GetCompanyDetail($webglobal['file_company']['editinfo']['id'], true, true);

			global_ShowMessage("恭喜您，地图定位成功！", WEBURLPATH."/mycom.php?action=map");
		}
	}
//商家信息
} else {
	$webglobal['file_company'] = array();
	$webglobal['file_company']['editinfo'] = $user;
	$webglobal['file_company']['editinfo']['description'] = str_replace("<br />", "\n", $webglobal['file_company']['editinfo']['description']);
	$webglobal['file_company']['category'] = $companyCategoryDataModel->formatCategoryOption(0,0);
	$webglobal['file_company']['area_json'] = $areaDataModel->formatClassToJson(0, 0);
	$webglobal['file_company']['area_tree'] = '';
	$webglobal['file_company']['imagepath'] = '/upfiles/company/'.global_FormatTime(0, 'Ym');
	$webglobal['file_company']['logosetting'] = $webglobal['file_company']['bcodesetting'] = array('type'=>array('gif', 'jpg', 'jpeg', 'png', 'bmp'), 'size'=>2097152);

	if (isset($webrequest['check']))
	{
		include WEBFILEPATH.'/include/upload.class.php';
		global_CheckPost(
				array(
					array('company_category_id,','int','isset($webglobal[\'file_company\'][\'category\'][@])','请选择商家分类',1),
					array('area_id,','int','','请选择对应的商圈',1),
					array('address,','str','','商家地址不能为空',1),
					array('tel,','str','','电话不能为空',1),
					array('site,','url','','',0,''),
					array('description,','','','商家介绍不能为空',1)
			));

		if (empty($webglobal['CPM']))
		{
			if (!isset($webglobal['areadata'][$webglobal['CPP']['area_id']])) {
				$webglobal['CPM']['area_id'] = '系统中不存在该商圈';
			}
			elseif ($webglobal['areadata'][$webglobal['CPP']['area_id']]['level'] != 3) {
				$webglobal['CPM']['area_id'] = '所选择的商圈不合法';
			} else {
				$parentArea = $webglobal['areadata'][$webglobal['CPP']['area_id']]['upCategoryID'];
				foreach ($parentArea as $areaId) {
					if (isset($webglobal['areadata'][$areaId])) {
						switch ($webglobal['areadata'][$areaId]['level']) {
							case 0:
								$webglobal['CPD']['province'] = $areaId;
								break;
							case 1:
								$webglobal['CPD']['city'] = $areaId;
								break;
							case 2:
								$webglobal['CPD']['district'] = $areaId;
								break;
						}
					}
				}
			}
		}

		if (!empty($webglobal['CPM'])) {
			global_ShowMessage($webglobal['CPM'], '', 'BACK');
		} else {
			$db->Update('company', '', "id='{$webglobal['file_company']['editinfo']['id']}'");
			//更新缓存
			cache_GetCompanyDetail($webglobal['file_company']['editinfo']['id'], true, true);

			global_ShowMessage("恭喜您，资料更新成功！", WEBURLPATH."/mycom.php");
		}
	}
	else
	{
		if (isset($webglobal['areadata'][$webglobal['file_company']['editinfo']['area_id']]))
		{
			$parentArea = $webglobal['areadata'][$webglobal['file_company']['editinfo']['area_id']]['upCategoryID'];
			array_shift($parentArea);
			$webglobal['file_company']['area_tree'] = implode(',', $parentArea) . ',' .$webglobal['file_company']['editinfo']['area_id'];
		}
	}
}

template_Display('mycom');
